package com.kedacom.freedomLabel.view.activity;

import android.graphics.Bitmap;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.model.bean.SharedFile;
import com.kedacom.freedomLabel.utils.IPUtil;
import com.kedacom.freedomLabel.view.activity.server.FileDownLoadUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class FileServerDownActivity extends AppCompatActivity {
    TextView showTips;
    Button beginServer;
    ImageView showZxing;
    private FileDownLoadUtil fileServer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_server_down);
        showTips =findViewById(R.id.show_tips);
        beginServer =findViewById(R.id.begin_server);
        showZxing =findViewById(R.id.show_zxing);


        beginServer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    fileServer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        List<SharedFile> sharedFileList = new ArrayList<>();
        SharedFile sharedFile01 = new SharedFile();
        sharedFile01.setName("人员.xlsx");
        sharedFile01.setPath(Environment.getExternalStorageDirectory() + "/mo.xlsx");
        sharedFileList.add(sharedFile01);
        fileServer = new FileDownLoadUtil(sharedFileList);
        String localIpStr = IPUtil.getLocalIpStr(getApplicationContext());

        showTips.setText("请在远程浏览器中输入:\n\n" + localIpStr + ":8080" );
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                Bitmap qrccode = QRCodeEncoder.syncEncodeQRCode(localIpStr + ":8080" ,400);
//                Bitmap qrccode = QRCodeEncoder.syncEncodeQRCode(ipStr + ":" + VideoServer.DEFAULT_SERVER_PORT+
//                        DEFAULT_FILE_PATH,400);
//                Bitmap qrccode = QRCodeEncoder.syncEncodeQRCode(ipStr + ":" + VideoServer.DEFAULT_SERVER_PORT,200);

                showZxing.setImageBitmap(qrccode);
            }
        });
    }

    @Override
    protected void onDestroy() {
        fileServer.stop();
        super.onDestroy();
    }
}
